package com.wtwd.campus.receiver;

import com.alibaba.fastjson.JSONObject;
import com.wtwd.campus.annotation.MsgType;
import com.wtwd.campus.common.Constants;
import com.wtwd.campus.dao.DeviceInfoMapper;
import com.wtwd.campus.entity.DeviceInfo;
import com.wtwd.campus.entity.MqAlarmResult;
import com.wtwd.campus.entity.MqLocationData;
import com.wtwd.campus.entity.MqMessage;
import com.wtwd.campus.h5_manager.entity.DeviceLog;
import com.wtwd.campus.h5_manager.service.DeviceLogService;
import com.wtwd.campus.service.DeviceAlarmService;
import com.wtwd.campus.service.DeviceInfoService;
import com.wtwd.campus.utils.ReceiverUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@MsgType(value = Constants.DELETE_CLOCK)
public class DelAlarmReceiver implements IReceiverHandler {
    @Autowired
    private DeviceInfoService deviceInfoService;
    @Autowired
    private ReceiverUtils receiverUtils;
    @Autowired
    private DeviceInfoMapper deviceInfoMapper;
    @Autowired
    private DeviceAlarmService deviceAlarmService;
    @Autowired
    private DeviceLogService deviceLogService;

    @Override
    public void handlerMessage(MqMessage mqMessage) {
        MqAlarmResult mqAlarmResult = (MqAlarmResult) mqMessage.getObject();
        String imei = mqAlarmResult.getImei();
        String commandNumber = mqAlarmResult.getCommandNumber();
        DeviceInfo deviceInfo = deviceInfoMapper.getDeviceInfoByImei(imei);
        //处理指令回复的时间
        String deviceDate = mqAlarmResult.getDeviceDate();
        String deviceTime = mqAlarmResult.getDeviceTime();
        Long time = getTime(deviceDate, deviceTime);
        if (deviceInfo != null) {
            MqLocationData mqLocationData = mqAlarmResult.getMqLocationData();
            if (mqLocationData != null) {
                if (mqLocationData.getLatitude() != 0 && mqLocationData.getLongitude() != 0) {
                    //进行定位数据存储以及围栏告警记录的存储
                    deviceInfo = receiverUtils.dealLocation(mqAlarmResult.getMqLocationData(), time, deviceInfo, 1);
                }
            }
            //修改设备信息
            deviceInfo.setLastUpTime(time);
            deviceInfoService.modifyDeviceInfo(deviceInfo);
            //将指令状态改为完成
            receiverUtils.completeCommandRecord(Constants.ALARM_CLOCK, imei, commandNumber, mqAlarmResult.getSequence(), Constants.COMMAND_COMPLETE, time);
            //删除缓存中的指令
            String delKey = Constants.ALARM_CLOCK.concat("-").concat(commandNumber).concat("-").concat(String.valueOf(mqAlarmResult.getSequence()));
            receiverUtils.deleteCommandInRedis(imei, delKey);
            //删除闹钟的信息
            deviceAlarmService.deleteAlarmByDeviceIdAndSeq(deviceInfo.getDeviceId(), mqAlarmResult.getSequence());
            //存储设备接收日志
            DeviceLog deviceLog = new DeviceLog();
            deviceLog.setCommandNumber(commandNumber);
            deviceLog.setSendUser(Constants.DEVICE_USER);
            deviceLog.setImei(imei);
            deviceLog.setBigType(Constants.MESS_TYPE);
            deviceLog.setType(Constants.DELETE_CLOCK);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("设备号", imei);
            jsonObject.put("闹钟序号", mqAlarmResult.getSequence());
            jsonObject.put("闹钟时间", mqAlarmResult.getTime());
            jsonObject.put("经度", mqLocationData.getLongitude());
            jsonObject.put("纬度", mqLocationData.getLatitude());
            jsonObject.put("闹钟开关", mqAlarmResult.getFlag());
            jsonObject.put("闹钟名称", mqAlarmResult.getName());
            jsonObject.put("重复时间", mqAlarmResult.getRepeatTime());
            jsonObject.put("设备时间", deviceTime);
            deviceLog.setCmd(jsonObject.toJSONString());
            deviceLog.setSendTime(System.currentTimeMillis());
            deviceLog.setState(1);
            deviceLogService.addDeviceLog(deviceLog);
        } else {
            saveUnInStorage(imei);
            receiverUtils.cloaseChannel(imei);
        }
    }
}
